package a02_Thread;

/**
 * @Author quan
 * @Description 线程中断
 * @Date 2023/4/28 12:51
 */
public class Demo5_Interrupted1 {
    //方式1：定义一个中断标识
    private static boolean isQuit = false;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            //如果是false则打印
            while (!isQuit){
                System.out.println("hello thread...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程任务结束");
        });
        thread.start();
        //主线程休眠3秒，模拟子线程正在处理任务
        Thread.sleep(3000);
        //设置中断标识位为true
        isQuit = true;
        //让子线程先结束
        Thread.sleep(1000);
        System.out.println("主线程结束");
    }
}
